//
//  HNQuickServiceController.m
//  baidaifu
//
//  Created by 朱传正 on 2016/12/23.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNQuickServiceController.h"
#import "HNDocterMsgCell.h"
#import "HNServiceDetailController.h"

@interface HNQuickServiceController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic,strong)NSMutableArray *consultArr;



@end

@implementation HNQuickServiceController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"咨询服务";
    

    
    [self.view setBackgroundColor:RGB(236, 240, 241)];
    
    //    self.tableView.bounces = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self initData];
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 0.5)];
    lineLabel.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lineLabel];
    
}


-(void)initData{
    [self getConcernList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma -mark TableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _consultArr.count;
    //    return [_dataSource count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *doctorDic = _consultArr[indexPath.row];
    
    static NSString *CellIdentifier = @"HNDocterMsgCell";
    HNDocterMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSString *imageStr = [NSString stringWithFormat:@"%@%@",url_Base,[doctorDic objectForKey:@"photo"]];
    [cell.cellImage sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"D99GN4P63Q4F@2x.png"]];
    cell.cellTitle.text = [NSString stringWithFormat:@"%@",[doctorDic objectForKey:@"nickname"]];
    cell.cellTime.text = [NSString stringWithFormat:@"%@",[doctorDic objectForKey:@"addTime"]];
    cell.cellTaskStr.text = [NSString stringWithFormat:@"%@",[doctorDic objectForKey:@"goodsName"]];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *doctorDic = _consultArr[indexPath.row];
    HNServiceDetailController *getMegVc = [[HNServiceDetailController alloc]init];
    getMegVc.goodsId = [NSString stringWithFormat:@"%@",[doctorDic objectForKey:@"goodsId"]];
    
    [self.navigationController pushViewController:getMegVc animated:YES];
    
    
}


-(void)getConcernList{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:_userId forKey:@"userId"];
//    [param setObject:@"1" forKey:@"goodsType"];
    [param setObject:@"1" forKey:@"cur"];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_get_consultList parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取医生详情失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSDictionary * obj = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            
            _consultArr = [obj objectForKey:@"result"];
            NSLog(@"------result = %@",_consultArr);
            
            [self.tableView reloadData];
            //            [self createContentScrollView];
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取咨询服务列表失败"];
    }];
    
    
}


@end
